package com.CST.service.impl.ys;

import com.CST.common.constant.Constants;
import com.CST.dto.ys.YsBusinessBudgetDataDTO;
import com.CST.dto.ys.YsManageBudgetDataDTO;
import com.CST.dto.ys.YsOrganizationDTO;
import com.CST.entity.ys.YsBusinessBudgetData;
import com.CST.entity.ys.YsManageBudgetData;
import com.CST.entity.ys.YsOrganization;
import com.CST.mapper.cst.ys.YsBusinessBudgetDataMapper;
import com.CST.mapper.cst.ys.YsManageBudgetDataMapper;
import com.CST.mapper.cst.ys.YsOrganizationMapper;
import com.CST.service.ys.YsOrganizationService;
import com.CST.util.HttpUtil;
import com.CST.util.PageHelperTool;
import com.CST.vo.ys.YsBusinessBudgetDataVO;
import com.CST.vo.ys.YsManageBudgetDataVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  预算编制数据服务实现类
 *
 * @author cw
 * @since 2023-05-15
 */
@Service
public class YsOrganizationServiceImpl extends ServiceImpl<YsOrganizationMapper, YsOrganization> implements YsOrganizationService {

    @Autowired
    private YsBusinessBudgetDataMapper businessMapper;

    @Autowired
    private YsManageBudgetDataMapper manageMapper;

    /**
     * 新增编制
     * @param year 参数
     * @return result
     */
    @Override
    public Boolean insertOrganization(Integer year) {
        YsOrganization dto = new YsOrganization();
        int count = baseMapper.selectByYear(year);
        if (count > 0){
            throw new RuntimeException("存在重复年份");
        }
        dto.setYear(year);
        dto.setCreateBy(HttpUtil.getRequestHeaderInfo(Constants.USER_NAME));
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateBy(HttpUtil.getRequestHeaderInfo(Constants.USER_NAME));
        dto.setUpdateTime(LocalDateTime.now());
        dto.setStatus(0);
        baseMapper.insert(dto);
        return true;
    }

    /**
     * 取消发布
     * @param id 参数
     * @return result
     */
    @Override
    public Boolean cancelRelease(Integer id) {
        YsOrganization ysOrganization = baseMapper.selectById(id);

        if (ysOrganization != null){
            baseMapper.deleteReByYear(ysOrganization.getYear());
            baseMapper.deleteManageReByYear(ysOrganization.getYear());

            // 删除发布后的整年数据 -- 业务
            baseMapper.deleteReByYear(ysOrganization.getYear());
            // 删除发布后管理职能的整年数据  -- 管理
            baseMapper.deleteManageReByYear(ysOrganization.getYear());
        }
        baseMapper.cancelRelease(id);
        return true;
    }

    /**
     * 完成编制
     * @param year 年
     * @return result
     */
    @Override
    public Boolean finishOrganization(Integer year) {
        return baseMapper.finishOrganization(year);
    }

    /**
     * 发布数据
     * @param year 年
     * @return result
     */
    @Override
    public Boolean releaseOrganizationData(Integer year) {
        YsBusinessBudgetDataDTO dto = new YsBusinessBudgetDataDTO();
        YsManageBudgetDataDTO dto1 = new YsManageBudgetDataDTO();
        dto.setYear(String.valueOf(year));
        dto1.setYear(String.valueOf(year));
        // 删除发布后的整年数据 -- 业务
        baseMapper.deleteReByYear(year);

        // 删除发布后管理职能的整年数据  -- 管理
        baseMapper.deleteManageReByYear(year);

        // 查询出发布数据
        List<YsBusinessBudgetDataVO> ysBusinessBudgetData = businessMapper.getBusinessDataList(dto);
        List<YsManageBudgetDataVO> ysManageBudgetData = manageMapper.getManageDataList(dto1);
        if (ysBusinessBudgetData.size() != 0){
            int index = ysBusinessBudgetData.size() % 20 == 0 ? ysBusinessBudgetData.size() / 20 : ysBusinessBudgetData.size() / 20 + 1;
            for (int i = 0; i < index; i++) {
                //stream流表达式，skip表示跳过前i*50条记录，limit表示读取当前流的前50条记录
                baseMapper.releaseBusinessOrganizationData(ysBusinessBudgetData.stream().skip(i * 20).limit(20).collect(Collectors.toList()));
            }
        }
        if (ysManageBudgetData.size() != 0){
            int index = ysManageBudgetData.size() % 20 == 0 ? ysManageBudgetData.size() / 20 : ysManageBudgetData.size() / 20 + 1;
            for (int i = 0; i < index; i++) {
                //stream流表达式，skip表示跳过前i*50条记录，limit表示读取当前流的前50条记录
                baseMapper.releaseManageOrganizationData(ysManageBudgetData.stream().skip(i * 20).limit(20).collect(Collectors.toList()));
            }
        }
        baseMapper.releaseOrganization(year);
        return true;
    }

    @Override
    public PageInfo<YsOrganization> getOrganizationList(YsOrganizationDTO dto) {
        return PageHelperTool.excutePageInfo(baseMapper.selectList(dto),dto.getPageNum(),dto.getPageSize());
    }
}
